package com.wang.exam.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.common.pojo.BasePojoEnum;
import com.wang.common.util.BeanUtil;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.exam.mapper.AtExamStatMapper;
import com.wang.exam.pojo.AtExamDesc;
import com.wang.exam.pojo.AtExamDescEnum;
import com.wang.exam.pojo.AtExamIntvig;
import com.wang.exam.pojo.AtExamIntvigQuery;
import com.wang.exam.pojo.AtExamStat;
import com.wang.exam.pojo.AtExamStatQuery;
import com.wang.exam.service.AtExamDescService;
import com.wang.exam.service.AtExamIntvigService;
import com.wang.exam.service.AtExamStatService;
import com.wang.users.pojo.AtUsersDesc;
import com.wang.users.pojo.AtUsersDescEnum;
import com.wang.users.pojo.AtUsersDescQuery;
import com.wang.users.service.AtUsersDescService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 针对表【监考统计表】的数据库操作Service实现
 *
 * @author ZjxMi
 */
@Log4j2
@Service
public class AtExamStatServiceImpl extends ServiceImpl<AtExamStatMapper, AtExamStat>
        implements AtExamStatService {

    @Autowired
    private BeanUtil beanUtil;
    @Autowired
    private AtExamDescService examDescService;
    @Autowired
    private AtExamIntvigService intvigService;
    @Autowired
    private AtUsersDescService usersDescService;

    /**
     * model2: jsp --> servlet -->service --> dao --> db, 我们自己封装了一个 PageInfoUtil;
     * MybatisPlus: 不再需要调用 PageInfoUtil,老代码改造的时候是为了整合代码,如果没有老代码,直接写的新代码,采用下面的方式来搞
     *
     * @param page
     * @param query 查询对象
     * @return
     */
    @Override
    public List<AtExamStat> findList(IPage<AtExamStat> page, AtExamStatQuery query) {
        if (page != null) {
            return this.baseMapper.findList(page, query);
        }
        return this.baseMapper.findList(query);
    }

    @Override
    public AtExamStat findOne(AtExamStatQuery query) {
        return this.baseMapper.findOne(query);
    }

    @Override
    public int updateBatch(AtExamStatQuery query) {
        return this.baseMapper.updateBatch(query);
    }

    @Override
    public int deleteBatch(AtExamStatQuery query) {
        return this.baseMapper.deleteBatch(query);
    }

    /**
     * 执行思路:
     * <pre>
     *     1. 查询所有的考试信息(要分页查询)
     *     2. 遍历所有的考试信息,只有考试是审核通过,并且为已结束,才能进行下一步
     *     3. 准备统计信息,进行入库
     * </pre>
     * @return
     */
    @Override
    public boolean proccedSaveBatch() {
        log.info("==统计监考次数开始==");
        /* 先删除数据 */
        this.deleteBatch(new AtExamStatQuery());
        IPage<AtExamIntvig> examDescPage = new Page<>(1, ConstatFinalUtil.SECOND);
        AtExamIntvigQuery examDescQuery = new AtExamIntvigQuery();
        AtExamIntvig examDescEntity = examDescQuery.getEntity();
        examDescEntity.setStatus(AtExamDescEnum.STATUSNEW_PASS.getCode());
        List<AtExamIntvig> examDescList = this.intvigService.findList(examDescPage, examDescQuery);
        log.info("==监考记录信息:总条数:{},总页数:{},每页条数:{}==", examDescPage.getTotal(), examDescPage.getPages(), examDescPage.getSize());
        for (int i = 1 ; i <= examDescPage.getPages() ; i++) {
            log.info("==监考记录信息:总条数:{},总页数:{},每页条数:{},当前页:{}==", examDescPage.getTotal(), examDescPage.getPages(),
                    examDescPage.getSize(), i);
            examDescPage.setCurrent(i);
            examDescList = this.intvigService.findList(examDescPage, examDescQuery);
            for (AtExamIntvig dataTemp : examDescList) {
                AtExamDesc exam = dataTemp.getExam();
                AtUsersDesc users = dataTemp.getUsers();

                AtExamStat examStatDb = new AtExamStat();
                examStatDb.setUsersId(dataTemp.getUsersId());
                if (AtExamDescEnum.DATESTATUS_WEEKEND.getCode().equals(exam.getDateStatus())) {
                    /* 周日工作量翻倍 */
                    examStatDb.setWeekendNum(2);
                } else {
                    examStatDb.setNum(1);
                }
                examStatDb.setWorkNum(examStatDb.getNum() + examStatDb.getWeekendNum());
                examStatDb.setName(users.getName());
                /* 保存数据库 */
                boolean dbFlag = this.save(examStatDb);
                log.info("==保存统计信息,结果:{}==", dbFlag);
            }
        }
        /* 查询所有用户,把未参加过监考的老师添加进去 */
        AtUsersDescQuery usersDescQuery = new AtUsersDescQuery();
        usersDescQuery.getEntity().setStatus(AtUsersDescEnum.STATUSNEW_ZAIZHI.getCode());
        IPage<AtUsersDesc> usersPage = new Page<>(1, ConstatFinalUtil.SECOND);
        List<AtUsersDesc> usersDescList = this.usersDescService.findList(usersPage, usersDescQuery);
        log.info("==未参加过监考的老师信息:总条数:{},总页数:{},每页条数:{}==", usersPage.getTotal(), usersPage.getPages(),
                usersPage.getSize());
        for(int i = 1 ; i <= usersPage.getPages() ; i++) {
            log.info("==未参加过监考的老师信息:总条数:{},总页数:{},每页条数:{},当前页:{}==", usersPage.getTotal(), usersPage.getPages(),
                    usersPage.getSize(), i);
            usersPage.setCurrent(i);
            usersDescList = this.usersDescService.findList(usersPage, usersDescQuery);
            for (AtUsersDesc dataTemp : usersDescList) {
                AtExamStat examStatDb = new AtExamStat();
                examStatDb.setUsersId(dataTemp.getId());
                examStatDb.setName(dataTemp.getName());
                /* 保存数据库 */
                boolean dbFlag = this.save(examStatDb);
                log.info("==未参加)==未参加过监考的老师信息,结果:{}==", dbFlag);
            }
        }
        log.info("==统计监考次数结束==");
        return true;
    }

    /**
     * 对数据额外处理(非用户填写的数据)
     * @param entity
     */
    private void proccedUpdateData(AtExamStat entity){
        if (Objects.isNull(entity.getCreateTime())) {
            entity.setCreateTime(new Date());
        }
        entity.setUpdateTime(new Date());
        /* 判断对象是否为空 */
        if (Objects.isNull(entity.getPubTime())) {
            entity.setPubTime(new Date());
        }
    }

    @Override
    public boolean save(AtExamStat entity) {
        this.proccedUpdateData(entity);
        /* 新增加的记录默认未删除 */
        entity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
        /* 根据用户信息进行唯一判断 */
        AtExamStatQuery statQuery = new AtExamStatQuery();
        AtExamStat statEntity = statQuery.getEntity();
        statEntity.setUsersId(entity.getUsersId());
        IPage<AtExamStat> statPage = new Page<>(1, ConstatFinalUtil.SECOND);
        List<AtExamStat> statList = this.findList(statPage, statQuery);
        if (statPage.getTotal() > 0) {
            /* 更新 */
            AtExamStat examStatDb = statList.get(0);
            examStatDb.setNum(entity.getNum() + examStatDb.getNum());
            examStatDb.setWeekendNum(entity.getWeekendNum() + examStatDb.getWeekendNum());
            examStatDb.setWorkNum(entity.getWorkNum() + examStatDb.getWorkNum());
            return super.updateById(examStatDb);
        } else {
            /* 添加 */
            entity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
            entity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
            return super.save(entity);
        }
    }

    @Override
    public boolean updateById(AtExamStat entity) {
        /* 先根据id查询数据库的内容; */
        AtExamStatQuery paramQuery = new AtExamStatQuery();
        AtExamStat entityParam = paramQuery.getEntity();
        entityParam.setId(entity.getId());
        /* 数据库中查询的结果 */
        AtExamStat oneDbResult = this.findOne(paramQuery);
        /*
        * 源对象和目标对象是一个类型(10个字段)
        * 源对象有5个字段有值,目标对象是10个有值
        * 把源对象中的5个字段有值,赋值到目标对象中.(覆盖)
        * */
        oneDbResult = (AtExamStat) this.beanUtil.copy(entity, oneDbResult);

        this.proccedUpdateData(oneDbResult);
        return super.updateById(oneDbResult);
    }
}